package com.zk.bcutil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {

	private static String url;  
    
   // private String url = "jdbc:oracle:thin:@10.41.50.23:1521:orcl";  

    private static String user;//oracle数据库的用户名  
    private static String password;//oracle数据库的用户密码  
    private PreparedStatement sta = null;  
    private ResultSet rs = null;  
    private static Connection conn = null;

	private static String driver = "oracle.jdbc.driver.OracleDriver";  
    
    
    
    static{
		try {
			 try {  
		            Class.forName(driver);  
		        } catch (ClassNotFoundException e) {  
		            e.printStackTrace();  
		        }  
			//加载属性文件，读取数据库连接配置信息
			Properties pro = new Properties();
			try {
				pro.load(DBUtil.class.getResourceAsStream("/jdbc.properties"));
			} catch (IOException e) {
				System.out.println("未找到配置文件！！！");
			}
			url = pro.getProperty("url");
			user = pro.getProperty("username");
			password = pro.getProperty("password");
			//driver = pro.getProperty("driver");
			conn = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//return conn;
	}

    /** 
     * 加载驱动程序 
     */  
   /* static {  
        try {  
            Class.forName(driver);  
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        }  
    } 
    */
    /** 
     * @param sql sql语句 
     * @param obj 参数 
     * @return 数据集合 
     */  
    public ResultSet Query(String sql){  
        conn=getConn();  
        try {  
            sta=conn.prepareStatement(sql);  
           /* if(obj!=null){  
                for(int i=0;i<obj.length;i++){  
                    sta.setObject(i+1, obj[i]);  
                }  
            }*/  
            rs=sta.executeQuery();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return rs;  
    }  
    
    public int update(String sql){  
        conn=getConn(); 
        int i = 0;
        try {  
            sta=conn.prepareStatement(sql);  
           /* if(obj!=null){  
                for(int i=0;i<obj.length;i++){  
                    sta.setObject(i+1, obj[i]);  
                }  
            }*/  
            i=sta.executeUpdate();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
        return i;  
    } 
    
    /** 
     * @return 连接对象 
     */  
    public Connection getConn() {  
        try {  
            conn = DriverManager.getConnection(url, user, password);  
        } catch (SQLException e) {  

            e.printStackTrace();  
        }  
        return conn;  
    } 
    
    /** 
     * 关闭资源 
     */  
    public void close() {  
        try {  
            if (rs != null) {  
                rs.close();  
            }  
        } catch (SQLException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (sta != null) {  
                    sta.close();  
                }  
            } catch (SQLException e2) {  
                e2.printStackTrace();  
            } finally {  
                if (conn != null) {  
                    try {  
                        conn.close();  
                    } catch (SQLException e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  
        }  
    }
}
